﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MemRecharge
    {
        public int Add(Chain.Model.MemRecharge model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemRecharge(");
            builder.Append("RechargeMemID,RechargeAccount,RechargeType,RechargeMoney,RechargeGive,RechargeRemark,RechargeShopID,RechargeCreateTime,RechargeUserID,RechargeCardBalance,RechargeIsApprove,RechargePoint)");
            builder.Append(" values (");
            builder.Append("@RechargeMemID,@RechargeAccount,@RechargeType,@RechargeMoney,@RechargeGive,@RechargeRemark,@RechargeShopID,@RechargeCreateTime,@RechargeUserID,@RechargeCardBalance,@RechargeIsApprove,@RechargePoint)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeMemID", SqlDbType.Int, 4), new SqlParameter("@RechargeAccount", SqlDbType.VarChar, 50), new SqlParameter("@RechargeType", SqlDbType.TinyInt, 1), new SqlParameter("@RechargeMoney", SqlDbType.Money, 8), new SqlParameter("@RechargeGive", SqlDbType.Money, 8), new SqlParameter("@RechargeRemark", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@RechargeShopID", SqlDbType.Int, 4), new SqlParameter("@RechargeCreateTime", SqlDbType.DateTime), new SqlParameter("@RechargeUserID", SqlDbType.Int, 4), new SqlParameter("@RechargeCardBalance", SqlDbType.Money, 8), new SqlParameter("@RechargeIsApprove", SqlDbType.Bit), new SqlParameter("@RechargePoint", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.RechargeMemID;
            cmdParms[1].Value = model.RechargeAccount;
            cmdParms[2].Value = model.RechargeType;
            cmdParms[3].Value = model.RechargeMoney;
            cmdParms[4].Value = model.RechargeGive;
            cmdParms[5].Value = model.RechargeRemark;
            cmdParms[6].Value = model.RechargeShopID;
            cmdParms[7].Value = model.RechargeCreateTime;
            cmdParms[8].Value = model.RechargeUserID;
            cmdParms[9].Value = model.RechargeCardBalance;
            cmdParms[10].Value = model.RechargeIsApprove;
            cmdParms[11].Value = model.RechargePoint;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataTable AgainPrint(int rechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RechargeMoney,RechargeGive,MemCard,MemName,RechargeCreateTime,UserName,RechargeAccount,MemMoney,RechargeCardBalance,RechargePoint ");
            builder.Append("from MemRecharge,Mem,SysUser ");
            builder.Append("where MemRecharge.RechargeMemID=Mem.MemID ");
            builder.Append("and MemRecharge.RechargeUserID=SysUser.UserID ");
            builder.Append("and RechargeID=@RechargeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = rechargeID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

        public bool Delete(int RechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemRecharge ");
            builder.Append(" where RechargeID=@RechargeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RechargeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string RechargeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemRecharge ");
            builder.Append(" where RechargeID in (" + RechargeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int RechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemRecharge");
            builder.Append(" where RechargeID=@RechargeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RechargeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string orderaccount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemRecharge");
            builder.Append(" where RechargeAccount=@RechargeAccount");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeAccount", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = orderaccount;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public decimal GetGiveMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(RechargeGive) FROM MemRecharge,Mem,MemLevel,SysShop,SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RechargeID,RechargeMemID,RechargeAccount,RechargeType,RechargeMoney,RechargeGive,RechargeRemark,RechargeShopID,RechargeCreateTime,RechargeUserID,RechargeCardBalance,RechargeIsApprove ,RechargePoint ");
            builder.Append(" FROM MemRecharge ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" RechargeID,RechargeMemID,RechargeAccount,RechargeType,RechargeMoney,RechargeGive,RechargeRemark,RechargeShopID,RechargeCreateTime,RechargeUserID,RechargeCardBalance,RechargeIsApprove,RechargePoint ");
            builder.Append(" FROM MemRecharge ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.RechargeID desc");
            }
            builder.Append(")AS Row, T.*  from MemRecharge T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "MemRecharge,Mem,MemLevel,SysShop,SysUser";
            string[] column = new string[] { "MemRecharge.*,(RechargeMoney-RechargeGive) as  RechargeOrdMoney,Mem.*,MemLevel.LevelName,SysShop.ShopName,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "RechargeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListSP1(int PageSize, int PageIndex, string IndexColumn, bool isAsc, out int resCount, params string[] strWhere)
        {
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            DataSet set = DbHelperSQL.GetTable(column, condition, IndexColumn, isAsc, PageSize, PageIndex, "CP_MemRechargeReport");
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListSP2(int PageSize, int PageIndex, string IndexColumn, bool isAsc, out int resCount, params string[] strWhere)
        {
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            DataSet set = DbHelperSQL.GetTable(column, condition, IndexColumn, isAsc, PageSize, PageIndex, "CP_ShopRechargeReport");
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("RechargeID", "MemRecharge");
        }

        public Chain.Model.MemRecharge GetModel(int RechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 RechargeID,RechargeMemID,RechargeAccount,RechargeType,RechargeMoney,RechargeGive,RechargeRemark,RechargeShopID,RechargeCreateTime,RechargeUserID,RechargeCardBalance,RechargeIsApprove,RechargePoint from MemRecharge ");
            builder.Append(" where RechargeID=@RechargeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RechargeID;
            Chain.Model.MemRecharge recharge = new Chain.Model.MemRecharge();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["RechargeID"] != null) && (set.Tables[0].Rows[0]["RechargeID"].ToString() != ""))
            {
                recharge.RechargeID = int.Parse(set.Tables[0].Rows[0]["RechargeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeMemID"] != null) && (set.Tables[0].Rows[0]["RechargeMemID"].ToString() != ""))
            {
                recharge.RechargeMemID = int.Parse(set.Tables[0].Rows[0]["RechargeMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeAccount"] != null) && (set.Tables[0].Rows[0]["RechargeAccount"].ToString() != ""))
            {
                recharge.RechargeAccount = set.Tables[0].Rows[0]["RechargeAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RechargeType"] != null) && (set.Tables[0].Rows[0]["RechargeType"].ToString() != ""))
            {
                recharge.RechargeType = int.Parse(set.Tables[0].Rows[0]["RechargeType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeMoney"] != null) && (set.Tables[0].Rows[0]["RechargeMoney"].ToString() != ""))
            {
                recharge.RechargeMoney = decimal.Parse(set.Tables[0].Rows[0]["RechargeMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeGive"] != null) && (set.Tables[0].Rows[0]["RechargeGive"].ToString() != ""))
            {
                recharge.RechargeGive = decimal.Parse(set.Tables[0].Rows[0]["RechargeGive"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeRemark"] != null) && (set.Tables[0].Rows[0]["RechargeRemark"].ToString() != ""))
            {
                recharge.RechargeRemark = set.Tables[0].Rows[0]["RechargeRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RechargeShopID"] != null) && (set.Tables[0].Rows[0]["RechargeShopID"].ToString() != ""))
            {
                recharge.RechargeShopID = int.Parse(set.Tables[0].Rows[0]["RechargeShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeCreateTime"] != null) && (set.Tables[0].Rows[0]["RechargeCreateTime"].ToString() != ""))
            {
                recharge.RechargeCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["RechargeCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeUserID"] != null) && (set.Tables[0].Rows[0]["RechargeUserID"].ToString() != ""))
            {
                recharge.RechargeUserID = int.Parse(set.Tables[0].Rows[0]["RechargeUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeCardBalance"] != null) && (set.Tables[0].Rows[0]["RechargeCardBalance"].ToString() != ""))
            {
                recharge.RechargeCardBalance = decimal.Parse(set.Tables[0].Rows[0]["RechargeCardBalance"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RechargeIsApprove"] != null) && (set.Tables[0].Rows[0]["RechargeIsApprove"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["RechargeIsApprove"].ToString() == "1") || (set.Tables[0].Rows[0]["RechargeIsApprove"].ToString().ToLower() == "true"))
                {
                    recharge.RechargeIsApprove = true;
                }
                else
                {
                    recharge.RechargeIsApprove = false;
                }
            }
            if ((set.Tables[0].Rows[0]["RechargePoint"] != null) && (set.Tables[0].Rows[0]["RechargePoint"].ToString() != ""))
            {
                recharge.RechargePoint = int.Parse(set.Tables[0].Rows[0]["RechargePoint"].ToString());
            }
            return recharge;
        }

        public DataSet GetRechargeByTime(DateTime starttime, DateTime endtime, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthRecharge", parameters, "#RechargeData");
        }

        public decimal GetRechargeMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(RechargeMoney) FROM MemRecharge,Mem,MemLevel,SysShop,SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetRecMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select sum(RechargeMoney-RechargeGive) from MemRecharge where RechargeMoney > 0 AND RechargeIsApprove=1 ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemRecharge ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MemRecharge model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemRecharge set ");
            builder.Append("RechargeMemID=@RechargeMemID,");
            builder.Append("RechargeAccount=@RechargeAccount,");
            builder.Append("RechargeType=@RechargeType,");
            builder.Append("RechargeMoney=@RechargeMoney,");
            builder.Append("RechargeGive=@RechargeGive,");
            builder.Append("RechargeRemark=@RechargeRemark,");
            builder.Append("RechargeShopID=@RechargeShopID,");
            builder.Append("RechargeCreateTime=@RechargeCreateTime,");
            builder.Append("RechargeUserID=@RechargeUserID,");
            builder.Append("RechargeCardBalance=@RechargeCardBalance,");
            builder.Append("RechargeIsApprove=@RechargeIsApprove,");
            builder.Append("RechargePoint=@RechargePoint ");
            builder.Append(" where RechargeID=@RechargeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RechargeMemID", SqlDbType.Int, 4), new SqlParameter("@RechargeAccount", SqlDbType.VarChar, 50), new SqlParameter("@RechargeType", SqlDbType.TinyInt, 1), new SqlParameter("@RechargeMoney", SqlDbType.Money, 8), new SqlParameter("@RechargeGive", SqlDbType.Money, 8), new SqlParameter("@RechargeRemark", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@RechargeShopID", SqlDbType.Int, 4), new SqlParameter("@RechargeCreateTime", SqlDbType.DateTime), new SqlParameter("@RechargeUserID", SqlDbType.Int, 4), new SqlParameter("@RechargeCardBalance", SqlDbType.Money, 8), new SqlParameter("@RechargeIsApprove", SqlDbType.Bit), new SqlParameter("@RechargePoint", SqlDbType.Int, 4), new SqlParameter("@RechargeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.RechargeMemID;
            cmdParms[1].Value = model.RechargeAccount;
            cmdParms[2].Value = model.RechargeType;
            cmdParms[3].Value = model.RechargeMoney;
            cmdParms[4].Value = model.RechargeGive;
            cmdParms[5].Value = model.RechargeRemark;
            cmdParms[6].Value = model.RechargeShopID;
            cmdParms[7].Value = model.RechargeCreateTime;
            cmdParms[8].Value = model.RechargeUserID;
            cmdParms[9].Value = model.RechargeCardBalance;
            cmdParms[10].Value = model.RechargeIsApprove;
            cmdParms[11].Value = model.RechargePoint;
            cmdParms[12].Value = model.RechargeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateRecharge(int rechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" update MemRecharge set RechargeIsApprove=1 where RechargeID={0} ", rechargeID);
            return DbHelperSQL.ExecuteSql(builder.ToString());
        }
    }
}

